﻿@using RooHui.Infrastructure.Entities.Entity;
@using RooHui.Tool;
@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Base.cshtml";
}
@section head{
   
    <script src="@Url.Content("~/Content/operate/usermanage.js")" type="text/javascript"></script>
}
<div id="toptoolbar" style="width: 100%">
</div>
<div style="width: 100%; clear: both" class="liger-form">
    <table cellpadding="0" cellspacing="0" class="l-table-edit">
        <tr>
            <td align="right" class="l-table-edit-td">
                用户名:
            </td>
            <td align="left" class="l-table-edit-td">
                <input name="txtUserCode" type="text" id="txtUserCode" ltype="text" />
            </td>
            <td align="right" class="l-table-edit-td">
                真实姓名:
            </td>
            <td align="left" class="l-table-edit-td">
                <input name="txtRealName" type="text" id="txtRealName" ltype="text" />
            </td>
            <td align="left" class="l-table-edit-td">
                <input type="button" value="查询" id="btnSearch" onclick="search(1)" class="l-button" />@*style="background: url('../../Content/roohui/ligerUI/skins/icons/search2.gif') no-repeat"*@
            </td>
        </tr>
    </table>
</div>
<div id="usergrid">
</div>
<form action="/" method="post" id="frmRole" name="frmRole" style="background-color: white;
display: none">
<div id="authorizetoolbar">
</div>
<div style="clear: both">
    <ul id="roletree">
    </ul>
</div>
<input type="hidden" name="selectuserName" id="selectuserName" value="" />
</form>
<form action="/" method="post" id="frmUser" name="frmUser" style="background-color: white;
display: none">
<div class="liger-form" style="padding-top: 10px">
    <table cellpadding="0" cellspacing="0" class="l-table-edit">
        <tr>
            <td align="right" class="l-table-edit-td">
                <span class="l-star">*</span>&nbsp;用户编码:
            </td>
            <td align="left" class="l-table-edit-td">
                <input name="txtUserName" type="text" id="txtUserName" ltype="text"  validate="{required:true,maxlength:10}"  
                   />
            </td>
            <td align="left">
            </td>
        </tr>
        <tr>
            <td align="right" class="l-table-edit-td">
                <span class="l-star">*</span>&nbsp;用户姓名:
            </td>
            <td align="left" class="l-table-edit-td">
                <input name="txtRealUserName" type="text" id="txtRealUserName" ltype="text" validate="{required:true}"
                   />
            </td>
            <td align="left">
            </td>
        </tr>
        <tr>
            <td align="right" class="l-table-edit-td">
                <span class="l-star"></span>&nbsp;办公电话:
            </td>
            <td align="left" class="l-table-edit-td">
                <input name="txtTelephone" type="text" id="txtTelephone" ltype="text" />
            </td>
            <td align="left">
            </td>
        </tr>
        <tr>
            <td align="right" class="l-table-edit-td">
                <span class="l-star">*</span>&nbsp;移动电话:
            </td>
            <td align="left" class="l-table-edit-td">
                <input name="txtHandphone" type="text" id="txtHandphone" ltype="text" validate="{required:true}"
                  />
            </td>
            <td align="left">
            </td>
        </tr>
        <tr>
        <td align="center" colspan="3">
            <span id="lblmsg" class="l-validate-error">&nbsp;</span>
            <input type="hidden" name="isEdit" id="isEdit" value="0" />
        </td>
    </tr>
    </table>
</div>
</form>
 <script src="@Url.Content("~/Content/roohui/jquery-validation/jquery.validate.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Content/roohui/jquery-validation/jquery.metadata.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Content/roohui/jquery-validation/messages_cn.js")" type="text/javascript"></script>
<script type="text/javascript">

    var usergrid;
    var saveDialog;
    var roletree;
    var roleDialog;
    var txtuserName;
    $(function () {
        txtuserName = $("#txtUserName").ligerGetTextBoxManager();
        $("#authorizetoolbar").ligerToolBar({ items: [
            { text: '授权', click: authorize, icon: "lock" },
            { text: '重置', click: reset_user_role, icon: "back" },
            { text: '刷新', click: refresh_user_role, icon: "refresh" }
            ]
        });

        $("#toptoolbar").ligerToolBar({ items: [{ text: '新增', click: grid_user_add, icon: "add" },
            { text: '修改', click: grid_user_edit, icon: "modify" },
            { text: '删除', click: grid_user_delete, icon: "delete" },
            { text: '刷新', click: grid_user_refresh, icon: "refresh" }
            ]
        });
        usergrid = $("#usergrid").ligerGrid({
            columns: [
                { display: '用户名', name: 'UserName', width: '20%', isSort: false },
                { display: '用户姓名', name: 'RealName', width: '20%', isSort: false },
                { display: '办公电话', name: 'TelePhone', width: '15%', isSort: false },
                { display: '移动电话', name: 'HandPhone', width: '15%', isSort: false },
                { display: '状态', name: 'Status', width: '10%', isSort: false,
                    render: function (item) {
                        if (parseInt(item.Status) == 0) return '禁用';
                        return '正常';
                    }
                },
                {
                    display: '操作', isAllowHide: false,
                    render: function (row) {

                        var html = '<a href="#" onclick="grid_user_show(\'' + row.UserName + '\',\'' + row.RealName + '\',\'' + row.TelePhone + '\',\'' + row.HandPhone + '\')">修改</a>&nbsp;&nbsp;<a href="#" onclick="user_delete(\'' + row.UserName + '\',\'' + row.RealName + '\')">删除</a>';
                        if (row.Status == 1) {
                            html = html + '&nbsp;&nbsp;<a href="#" onclick="changestate(\'' + row.UserName + '\',0,\'' + row.RealName + '\')">禁用</a>';
                        }
                        else {
                            html = html + '&nbsp;&nbsp;<a href="#" onclick="changestate(\'' + row.UserName + '\',1,\'' + row.RealName + '\')">启用</a>';
                        }
                        html = html + '&nbsp;&nbsp;<a href="#" onclick="grid_user_authorize(\'' + row.UserName + '\',\'' + row.RealName + '\')">授权</a>';
                        return html;
                    }
                }
                ],
            width: '100%', height: '100%', rownumbers: true,
            dataAction: 'server',
            url: "/User/GetUserListByPage",
            pageSizeOptions: [10],
            pageSize: 10
        });
        initRoleTree();

        $.metadata.setType("attr", "validate");
        var v = $("#frmUser").validate({
            errorPlacement: function (lable, element) {
                if (element.hasClass("l-textarea")) {
                    element.ligerTip({ content: lable.html(), target: element[0] });
                }
                else if (element.hasClass("l-text-field")) {
                    element.parent().ligerTip({ content: lable.html(), target: element[0] });
                }
                else {
                    lable.appendTo(element.parents("td:first").next("td"));
                }
            },
            success: function (lable) {
                lable.ligerHideTip();
                lable.remove();
            },
            submitHandler: function () {
                if ($('#txtUserName').val() == "") {
                    $('#txtUserName').focus();
                    $("#lblmsg").html('用户名不能为空');
                    return;
                }
                if ($('#txtRealUserName').val() == "") {
                    $('#txtRealUserName').focus();
                    $("#lblmsg").html('真实姓名不能为空');
                    return;
                }
                if ($('#txtHandphone').val() == "") {
                    $('#txtHandphone').focus();
                    $("#lblmsg").html('手机不能为空');
                    return;
                }
                $.ajax({
                    url: "/User/UserSave",
                    data: "userName=" + $('#txtUserName').val() + "&realName=" + $("#txtRealUserName").val()
                    + "&telePhone=" + $("#txtTelephone").val() + "&handPhone=" + $("#txtHandphone").val()
                    + "&optype=" + $("#isEdit").val(),
                    type: "post",
                    success: function (result) {
                        //写提交ajax操作
                        if (result == "1") {
                            $.ligerDialog.success("保存成功。");
                            user_reset();
                            saveDialog.hide();
                            grid_user_refresh();
                        }
                        else {
                            if (result == "-1") {
                                $.ligerDialog.error("用户名已存在!");
                            }
                            else {
                                $.ligerDialog.error("保存失败。");
                            }
                        }

                    }
                });
            }
        });
        $("#frmUser").ligerForm();
    }); 
    
    function initRoleTree() {
        roletree = $("#roletree").ligerTree({
            data: null,
            checkbox: true,
            slide: false,
            nodeWidth: 120,
            idFieldName: 'ID',
            parentIDFieldName: 'ParentID',
            textFieldName: 'Name',
            iconUrl: "/content/images/customers.gif",
            btnClickToToggleOnly: false,
            topParentIDValue: -1,
            attribute: ['ID'],
            isReturnParent: true,
            onSelect: function (node) {

            }
        });
    }
    function refresh_user_role() {
        var username = $("#selectuserName").val();
        getUserRole(username);
    }
    function reset_user_role() {

        var username = $("#selectuserName").val();
        $.ligerDialog.confirm('重置将清空用户的权限，是否继续？', '温馨提示', function (result) {
            if (result) {
                $.ajax({
                    url: "/User/UserAuthorizeDelete",
                    data: 'userName=' + username,
                    type: 'Post',
                    success: function (result) {
                        if (result == "1") {
                            $.ligerDialog.success('重置成功，请及时授权！', '温馨提示');
                            getUserRole(username);
                        }
                        else {
                            $.ligerDialog.warn('重置失败！', '温馨提示');
                        }
                    }
                });
            }
        });
    }
    function getUserRole(userName) {

        $.ajax({
            url: "/User/GetRoleByUserName",
            data: 'userName=' + userName,
            type: 'Post',
            success: function (result) {
                roletree.setData(eval("(" + result + ")"));
            }
        });
    }

    function grid_user_authorize(userName, realName) {
        //location.href =decodeURIComponent("/User/UserAdd") ;
        $("#selectuserName").val(userName);
        getUserRole(userName);
        roleDialog = $.ligerDialog.open({
            title: '给用户【' + realName + '】授权',
            id: 'userauthorizeDialog',
            name: 'userauthorizeDialog',
            slide: false,
            isHidden: false,
            modal: true,
            target: $("#frmRole"),
            width: 450, height: 350, isResize: true, allowClose: false,
            buttons: [
                    { text: '保存', onclick: authorize },
                    {
                        text: '取消', onclick: function (item, dialog) {
                            dialog.hide();
                        }
                    }
                ]
        });

    }

    function authorize() {
        if (roletree.getChecked() == null) {
            $.ligerDialog.warn('请选择角色！', '温馨提示');
            return;
        }

        var notes = roletree.getChecked();
        var ids = "";
        if (notes.length == 0) {
            $.ligerDialog.warn('您还没有选择角色。', '温馨提示');
            return;
        }
        for (var i = 0; i < notes.length; i++) {
            ids += notes[i].data.ID + ",";
        }
        ids = ids.substring(0, ids.length - 1);
        var username = $("#selectuserName").val();

        $.ajax({
            url: "/User/UserAuthorize",
            data: 'userName=' + username + '&roleIDs=' + ids,
            type: 'Post',
            success: function (result) {
                if (result == 1) {
                    $.ligerDialog.success('授权成功！', '温馨提示');
                    roleDialog.hide();
                }
                else {
                    $.ligerDialog.warn('授权失败！', '温馨提示');
                }
            }
        });

    }    
    
         
  
</script>
